package com.jcark.utils

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.TextView
import com.blankj.utilcode.util.DesensitizedUtil
import com.blankj.utilcode.util.RegexUtils
import com.jcark.utilscode.MToastUtils
import com.jcark.utilscode.listener.DebounceClickListener
import com.jcark.utilscode.listener.WithCheckNetworkClickListener
import com.jcark.utilscode.listener.setDebounceClickListener
import com.jcark.utilscode.listener.setOnClickWithCheckNetworkListener
import java.text.SimpleDateFormat
import java.util.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.d("KING", "15==" + DesensitizedUtil.desensitizedIdNumber(4, "123456789123456"))
        Log.d("KING", "18==" + DesensitizedUtil.desensitizedIdNumber(4, "123456789123456789"))

        Log.d("KING", "18==" + DesensitizedUtil.desensitizedIdNumber(20, "123456789123456789"))

        Log.d("KING", "My的名字-009不是King==" + RegexUtils.isChinese("My的名字-009不是King"))
        Log.d("KING", "My的名字是超==" + RegexUtils.isChinese("My的名字是超"))
        Log.d("KING", "我的名字是中文的==" + RegexUtils.isChinese("我的名字是中文的"))
        //val regEx = "[\u4E00-\u9FA5]"
        Log.d("KING", "--我的名字是King==" + RegexUtils.isRegex("我的名字是King", "[^\u4E00-\u9FA5]"))
        Log.d("KING", "--我的名字是中文的==" + RegexUtils.isRegex("我的名字是中文的", "[^\u4E00-\u9FA5]"))
        Log.d("KING", "--手机号=020155=" + RegexUtils.isMobileExact("020155"))
        Log.d("KING", "--手机号=110=" + RegexUtils.isMobileExact("110"))
        Log.d("KING", "--手机号=20265325580=" + RegexUtils.isMobileExact("20265325580"))
        Log.d("KING", "--手机号=202 6532 5580=" + RegexUtils.isMobileExact("202 6532 5580"))
        Log.d("KING", "--手机号=302658799881=" + RegexUtils.isMobileExact("302658799881"))
        Log.d("KING", "--手机号=13365201487=" + RegexUtils.isMobileExact("13365201487"))
        Log.d("KING", "--手机号=1336520148=" + RegexUtils.isMobileExact("1336520148"))

        Log.d("KING", "--手机号=151 65325580=" + RegexUtils.isMobileExact("151 65326666"))
        Log.d("KING", "--手机号=15165326666=" + RegexUtils.isMobileExact("15165326666"))
        Log.d("KING", "--手机号=151 6532 1235=" + RegexUtils.isMobileExact("151 6532 1235"))


        val commonCheck =
            "^(?![0-9]+$)(?![a-zA-Z]+$)(?![\\`\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\_\\+\\-\\=\\{\\}\\|\\[\\]\\\\\\;\\'\\:\\\"\\,\\.\\/\\<\\>\\?]+$)[0-9A-Za-z\\`\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\_\\+\\-\\=\\{\\}\\|\\[\\]\\\\\\;\\'\\:\\\"\\,\\.\\/\\<\\>\\?]{6,18}$"

        Log.d("KING", "--222=" + RegexUtils.isMatch(commonCheck, "222"))
        Log.d("KING", "--12345678=" + RegexUtils.isMatch(commonCheck, "12345678"))
        Log.d("KING", "--123aa=" + RegexUtils.isMatch(commonCheck, "12345aa"))
        Log.d("KING", "--1234567a=" + RegexUtils.isMatch(commonCheck, "1234567a"))
        Log.d("KING", "--123456789987654321q=" + RegexUtils.isMatch(commonCheck, "123456789987654321q"))
        Log.d("KING", "--12345678998wq54321q=" + RegexUtils.isMatch(commonCheck, "12345678998wq54321q"))

        var etName = findViewById<EditText>(R.id.etName)
        findViewById<TextView>(R.id.countNumber).setOnClickListener {
            MToastUtils.shortShow(this, "个数=" + etName.text.length)
        }

        Log.d("KING", "aa=${AppThemeName.newYear2023},name" + AppThemeName.newYear2023.name)

        val milltime: Float = 60 * 24 * 60 * 60 * 1000f
        Log.d("KING", "mitimeL=" + milltime)

        val etNumber = findViewById<EditText>(R.id.etNumber)
        etNumber.filters = arrayOf(InputFilterMinMax(1, 50))

        val addTextView = findViewById<TextView>(R.id.add)
        var iCount = 0
        addTextView.setOnClickListener(object : DebounceClickListener() {
            override fun onViewClick(v: View) {
                addTextView.text = "第【${iCount++}】次点击！"
                Log.d("KING", "count ===$iCount")
            }

        })

        addTextView.setDebounceClickListener({
            Log.d("KING", "BBBBBBBBBBBBBBB===${iCount++}")
        }, 1000L, isCheckNetwork = true, onNetworkInvalid = {
            Log.d("KING", "network error")
        })

        addTextView.setDebounceClickListener(object : DebounceClickListener() {
            override fun onViewClick(v: View) {
                Log.d("KING", "cccccccccccccccc===${iCount++}")
            }
        }, 300L, isCheckNetwork = true, "网络跑了~")

        addTextView.setDebounceClickListener(object :
            WithCheckNetworkClickListener("你的网络跟别人跑了~") {
            override fun onNetworkAvailable(v: View) {
                Log.d("KING", "aaaaaaaaaaa=checkNetwork==${iCount++}")
            }
        }, 1500L)

        addTextView.setOnClickWithCheckNetworkListener({
            addTextView.text = "第【${iCount++}】次点击！"
            Log.d("KING", "count ===$iCount")
        }, {
            MToastUtils.shortShow(message = "中场休息！~")
        },"无网络")
        Log.d("KING",">>>>>>>>time:30==${formatTime(1183440,"HH:mm:ss")}")
        Log.d("KING",">>>>>>>>time:90==${formatTime(90,"HH:mm:ss")}")
        Log.d("KING",">>>>>>>>time:0==${formatTime(0,"HH:mm:ss")}")
        Log.d("KING",">>>>>>>>time:-30==${formatTime(-30,"HH:mm:ss")}")

        val mutTextView = findViewById<TextView>(R.id.tv_mult_color)
        mutTextView.text = getSeatTextString()
    }

    fun formatTime(time: Long, pattern: String?): String? {
        val simpleDateFormat = SimpleDateFormat(pattern)
        simpleDateFormat.timeZone = TimeZone.getTimeZone("GMT +8:00")
        return simpleDateFormat.format(Date(time))
    }

    fun getSeatTextString(): SpannableString {
        var str = "抢"
        var defaultTextColorInt = Color.parseColor("#494F61")
        //var secondColorInt: Int? = null
        var  secondColorInt = Color.parseColor("#FF7E00")

        var seatString = "二等座 $str"
        val spannableString = SpannableString(seatString)
        if (null == secondColorInt) {
            //同色
            spannableString.setSpan(
                ForegroundColorSpan(defaultTextColorInt),
                0,
                seatString.length,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        } else {
            //不同色
            val index = seatString.indexOfFirst { it == ' ' }
            spannableString.setSpan(
                ForegroundColorSpan(defaultTextColorInt),
                0,
                index,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            spannableString.setSpan(
                ForegroundColorSpan(secondColorInt),
                index,
                seatString.length,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        }

        return spannableString
    }
}

enum class AppThemeName {
    default,
    newYear2023,
    springFestival2023
}